package com.zcxy.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxy.core.bean.PageQuery;
import com.zcxy.mybatis.service.BaseServiceImpl;
import com.zcxy.sys.entity.VerificationCode;
import com.zcxy.sys.mapper.VerificationCodeMapper;
import com.zcxy.sys.pojo.VerificationCodeVo;
import com.zcxy.sys.service.IVerificationCodeService;
import com.zcxy.util.BeanCopierUtil;
import com.zcxy.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * 邮箱验证码表Service业务层处理
 *
 * @author zcc
 * @email 860474958@qq.com
 * @date 2020-06-08
 */
@Service
@Slf4j
public class VerificationCodeServiceImpl extends BaseServiceImpl<VerificationCodeMapper, VerificationCode>
        implements IVerificationCodeService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public IPage<VerificationCodeVo> queryPageList(PageQuery<VerificationCode> queryVo) {
        QueryWrapper queryWrapper = new QueryWrapper(queryVo.getT() == null ? new VerificationCode() : queryVo.getT());
        Page page = new Page(queryVo.getPageNo(), queryVo.getPageSize());
        IPage<VerificationCode> sysUserVoIPage = baseMapper.selectPage(page, queryWrapper);
        return BeanCopierUtil.copyPage(sysUserVoIPage, VerificationCodeVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<VerificationCodeVo> queryList(VerificationCode queryVo) {

        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addVerificationCode(VerificationCode queryVo) {
        //TODO 具体新增业务限制各自实现
        baseMapper.insert(queryVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateVerificationCode(VerificationCode queryVo) {
        //查询数据库的实体(主键校验在controller的校验器里面)
        VerificationCode verificationCodeDb = baseMapper.selectById(queryVo.getId());
        BeanCopierUtil.copyObject(queryVo, verificationCodeDb);
        baseMapper.updateById(verificationCodeDb);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeVerificationCode(String id) {
        baseMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeAllVerificationCode(List<String> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        baseMapper.delete(queryWrapper);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void download(VerificationCode queryVo, HttpServletResponse response) throws IOException {
        List<VerificationCode> verificationCodes = baseMapper.selectList(new QueryWrapper<>(queryVo));
        List<Map<String, Object>> list = new ArrayList<>();
        for (VerificationCode verificationCodeExcel : verificationCodes) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("ID", verificationCodeExcel.getId());
            map.put("验证码", verificationCodeExcel.getCode());
            map.put("状态：1有效、0过期", verificationCodeExcel.getStatus());
            map.put("验证码类型：email或者短信", verificationCodeExcel.getType());
            map.put("接收邮箱或者手机号码", verificationCodeExcel.getValue());
            map.put("业务名称：如重置邮箱、重置密码等", verificationCodeExcel.getScenes());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


}
